﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Common;
using Common.Unit;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Razor.TagHelpers;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Web.Models.MvcExtension;

namespace Web
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = Configuration;

            //读取配置文件
            var builder = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);

            Configuration = builder.Build();

        }

        public IConfiguration Configuration { get; }

        // 该方法在运行时被调用。
        // 可以使用该方法将服务添加到容器中
        public void ConfigureServices(IServiceCollection services)
        {
            //后去配置文件中的连接数据库字符
            //var connection = Configuration.GetConnectionString("DefaultConnection");
            //AppSetting.GetConfig = connection;

            services.Configure<CookiePolicyOptions>(options =>
            {
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });
            //services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
            //注入session
            services.AddDistributedMemoryCache();

            services.AddSession(options =>
            {
                // Set a short timeout for easy testing.
                //超时就会清除掉
                options.IdleTimeout = TimeSpan.FromMinutes(30);
                //是否仅服务器端可读
                options.Cookie.HttpOnly = true;
            });
        }

        // 该方法在运行时被调用
        // 可以使用该方法来配置 HTTP 请求管道
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }
            //使用Cookies
            app.UseCookiePolicy(new CookiePolicyOptions()
            {
                   
            });
            //使用session
            app.UseSession();
            //静态文件
            //app.UseStaticFiles();
            //app.UseCookiePolicy();
            //整合上两个
            app.UseFileServer();

            //设定MVC路由规则
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });

            //设置公用appsetting
            ConfigurationUtil.SetConfiguration(Configuration);
        }
    }
}
